package com.example.wanziyi

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import java.io.FileNotFoundException

class HistoryActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history)

        val listView = findViewById<ListView>(R.id.prime_list_view)

        // 从文件加载素数列表
        val primeList = loadPrimes()

        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, primeList.map { it.toString() })
        listView.adapter = adapter
    }

    private fun loadPrimes(): List<Int> {
        val primeList = mutableListOf<Int>()
        try {
            val fis = openFileInput("primes.txt")
            fis.use { input ->
                val data = input.readBytes()
                primeList.addAll(data.toString().split(", ").map { it.toInt() })
            }
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return primeList
    }
}